package org.jiamandan.com.strategy.impl.tram.cmd;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDStopDto;
import org.jiamandan.com.entity.RechargeGun;
import org.jiamandan.com.service.IRechargeGunService;
import org.jiamandan.com.strategy.impl.tram.TramBasicService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd

 * @see:
 * @创建日期：2022-11-30 15:26
 * @功能说明：5.3. CMD=105 下发停止充电
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-11-30 15:26	         Jmd        	创建
 * @end
 */
@Service
@AllArgsConstructor
public class ZDIssueStopService implements TramBasicService {

    private IRechargeGunService iRechargeGunService;

    @Override
    public void doExecute(byte[] data, Channel client) {
        ZDStopDto dto = JSON.parseObject(CommonUtil.bytesToString(data), ZDStopDto.class);
        RechargeGun gun = iRechargeGunService.getById(dto.getGunId());
        if (gun != null) {
            StringBuilder builder = new StringBuilder();
            //充电枪编号
            builder.append(StringUtils.rightPad(CommonUtil.toHexString(gun.getGunNo()), 2, '0'));
            // 停止操作者 0：用户主动停止 1：平台强制停止
            builder.append(StringUtils.rightPad(Integer.toHexString(dto.getStopOperator()), 2, '0'));
            String payload = ZDObjectUtil.splicingMessage(builder.toString(), gun.getPileNo(), BizConstant.Cmd.CMD_105);
            client.writeAndFlush(payload);
        }
    }
}
